local kuangxiao = fk.CreateSkill {
  name = "th_kuangxiao",
  tags = { Skill.Compulsory },
}

kuangxiao:addEffect(fk.EventPhaseStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if target == player and not player.dead then
      return player:hasSkill(kuangxiao.name) and player.phase == Player.Play
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"draw2"}
    if player:isWounded() then
      local recover = "recover"
      table.insert(choices, recover)
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = kuangxiao.name,
      cancelable = false,
    })
    if choice == "draw2" then
      player:drawCards(2, kuangxiao.name)
    else
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = kuangxiao.name
      })
    end
    room:setPlayerMark(player, "_th_kuangxiao-phase", 1)
  end
})
kuangxiao:addEffect(fk.EventPhaseEnd, {
  anim_type = "negative",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and not player.dead then
      return player:getMark("_th_kuangxiao-phase") ~= 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "_th_kuangxiao-phase", 0)
    return #room.logic:getEventsOfScope(GameEvent.ChangeHp, 1, function (e)
      local damage = e.data.damageEvent
      if damage and target == damage.from then
        return true
      end
    end, Player.HistoryPhase) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:loseHp(player, 1, kuangxiao.name)
    if player.dead then return false end
    room:askToUseVirtualCard(player, {
      name = "slash",
      subcards = nil,
      skill_name = kuangxiao.name,
      prompt = "#th_kuangxiao-ask",
      cancelable = false,
      extra_data = {
        bypass_distances = true,
        bypass_times = true
      }
    })
  end
})

Fk:loadTranslationTable{
  ["th_kuangxiao"] = "狂咲",
  [":th_kuangxiao"] = "锁定技，出牌阶段开始时，你摸两张牌或回复1点体力，本阶段结束时，若你本阶段内未造成过伤害，你失去一点体力并视为使用一张无距离限制的【杀】。",

  ["#th_kuangxiao-ask"] = "狂咲：你须视为使用一张无距离限制的【杀】",
}

return kuangxiao